<?php
namespace app\common\model;

use think\Session;
use traits\model\SoftDelete;

class AuthGroup extends Base
{
    use SoftDelete;
    protected $deleteTime = 'delete_time'; // 软删除字段
    protected $autoWriteTimestamp = true;


    public function getRuleList($uid = null)
    {
        $uid = is_null($uid) ? $this->id : $uid;
        static $_rulelist = []; //保存用户验证通过的权限列表
        if (isset($_rulelist[$uid]))
        {
            return $_rulelist[$uid];
        }
        // 读取用户规则节点
        $ids = $this->getRuleIds($uid);
        if (empty($ids))
        {
            $_rulelist[$uid] = [];
            return [];
        }

        // 筛选条件
        $where = [
            'status' => 1
        ];
        if (!in_array('*', $ids))
        {
            $where['id'] = ['in', $ids];
        }
        //读取用户组所有权限规则
        $this->rules = collection(AuthRule::where($where)->select())->toArray();
        //循环规则，判断结果。
        $rulelist = []; //
        if (in_array('*', $ids))
        {
            $rulelist[] = "*";
        }
        foreach ($this->rules as $rule)
        {
            //只要存在就记录
            $rulelist[$rule['id']] = strtolower($rule['name']);
        }
        $_rulelist[$uid] = $rulelist;
        return array_unique($rulelist);
    }

    public function getUserInfo($uid = null)
    {
        $uid = is_null($uid) ? $this->id : $uid;

        return $uid != $this->id ? Admin::get(intval($uid)) : Session::get('admin');
    }

    public function getGroups($uid = null)
    {
        $uid = is_null($uid) ? 0 : $uid;
        static $groups = [];
        if (isset($groups[$uid]))
        {
            return $groups[$uid];
        }

        // 执行查询
        $roleId = $this->roleId;
        if(!$roleId){
            $arrUser = $this->getUserInfo($uid);
            $roleId = $arrUser["roleId"];
        }
        if(!$roleId){
            return [];
        }
        $user_groups = AuthGroup::get(['id' => $roleId])->toArray();
        $groups[$uid][] = $user_groups ? $user_groups : [];
        return $groups[$uid];
    }

    public function getRuleIds($id = null)
    {
        $id = is_null($id) ? 0 : $id;

        if(!$id){
            $ids = [];
        }else{
            $group_info = $this->where(['id'=>$id])->find();
            $ids = explode(',',$group_info['rules']);
        }

        return $ids;
    }

    public function isSuperAdmin($id = null)
    {
        $id = is_null($id) ? 0 : $id;

        if(!$id){
            return false;
        }else{
            $group_info = $this->where(['id'=>$id])->find();
            if($group_info['rules'] == '*'){
                return true;
            }else{
                return false;
            }
        }
    }

    /**
     * 获取管理员所属于的分组ID
     * @param int $uid
     * @return array
     */
    public function getGroupIds($uid = null)
    {
        $groups = $this->getGroups($uid);
        $groupIds = [];
        foreach ($groups as $K => $v) {
            $groupIds[] = (int)$v['group_id'];
        }
        return $groupIds;
    }
}